/**
 * 
 */
package com.xb.cloud.merchantapi.product.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xb.cloud.gateway.client.ReturnCodes;
import com.xb.cloud.gateway.client.ReturnUtils;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.merchantapi.product.service.ShopNavigationGroupApiService;
import com.xb.cloud.product.merchant.service.client.EndMerchantUserService;
import com.xb.cloud.product.merchant.service.client.ShopNavigationGroupService;
import com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo;
import com.xb.cloud.product.merchant.service.client.bo.ShopNavSkuRequestBo;
import com.xb.cloud.product.merchant.service.client.input.bo.ShopNavigationGroupInput;
import com.xb.cloud.product.service.business.model.ShopNavigationGroup;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;

/**
 * 
 * 店铺内分类API
 * 
 * @author kexue
 *
 */
@Service
public class ShopNavigationGroupApiServiceImpl implements ShopNavigationGroupApiService {

	@Resource
	private ShopNavigationGroupService shopNavigationGroupService;

	@Resource
	private EndMerchantUserService endMerchantUserService;

	/**
	 * 将sku加入店铺多个分组中
	 * 
	 * @param skuId
	 * @param groupIdList
	 * @param endMerchantUserBo
	 * @return
	 */
	public RtnInfo createShopNavicationGroupSkuRef(Long skuId, List<Long> groupIdList, Long endMerchantUserId) {
//		EndMerchantUserBo endMerchantUserBo = null;
//
//		// 获取当前merchantId对应的商家用户
//		ServiceReturnInfo merchantReturnInfo = endMerchantUserService.getEndMerchantUserById(endMerchantUserId);
//		if (ServiceReturnUtils.isSuccess(merchantReturnInfo)) {
//			if (merchantReturnInfo.getData() == null) {
//				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
//			} else {
//				endMerchantUserBo = (EndMerchantUserBo) merchantReturnInfo.getData();
//			}
//		} else {
//			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
//		}
//
//		// 插入关系数据
//		ServiceReturnInfo rtnInfo = shopNavigationGroupService.createShopNavicationGroupSkuRef(skuId, groupIdList,
//				endMerchantUserBo);
//		if (ServiceReturnUtils.isSuccess(rtnInfo)) {
//			return ReturnUtils.success();
//		} else {
//			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
//		}
		return null;

	}

	/**
	 * 创建店铺分组
	 * 
	 * @param groupInput
	 * @param endMerchantUserBo
	 * @return
	 */
	public RtnInfo createShopNavicationGroup(ShopNavigationGroupInput groupInput, Long endMerchantUserId) {

		EndMerchantUserBo endMerchantUserBo = null;

		// 获取当前merchantId对应的商家用户
		ServiceReturnInfo merchantReturnInfo = endMerchantUserService.getEndMerchantUserById(endMerchantUserId);
		if (ServiceReturnUtils.isSuccess(merchantReturnInfo)) {
			if (merchantReturnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				endMerchantUserBo = (EndMerchantUserBo) merchantReturnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
		
		
		ServiceReturnInfo rtnInfo = shopNavigationGroupService.createShopNavicationGroup(groupInput, endMerchantUserBo);
		if (ServiceReturnUtils.isSuccess(rtnInfo)) {
			return ReturnUtils.success();
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

	/**
	 * 查看某个店铺分组树形数据(由小到大)
	 * 
	 * @param Long
	 *            endMerchantUserId
	 * @return
	 */
	public RtnInfo getShopNavicationGroup(Long endMerchantUserId) {

		EndMerchantUserBo endMerchantUserBo = null;

		// 获取当前merchantId对应的商家用户
		ServiceReturnInfo merchantReturnInfo = endMerchantUserService.getEndMerchantUserById(endMerchantUserId);
		if (ServiceReturnUtils.isSuccess(merchantReturnInfo)) {
			if (merchantReturnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				endMerchantUserBo = (EndMerchantUserBo) merchantReturnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
		
		
		ServiceReturnInfo rtnInfo = shopNavigationGroupService.getShopNavicationGroup(endMerchantUserBo.getShopId());
		if (ServiceReturnUtils.isSuccess(rtnInfo)) {
			return ReturnUtils.success(rtnInfo.getData());
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

	@Override
	public RtnInfo deleteShopNavicationGroup(String groupId) {
		// 先查询是否是根节点，是的话查出子列表，否直接删除
		ServiceReturnInfo serviceReturnInfo = shopNavigationGroupService.getShopNavicationGroup(groupId);
		ShopNavigationGroup shopNavigationGroup = null;
		ServiceReturnInfo rtnInfo = null;
		if(ServiceReturnUtils.isSuccess(serviceReturnInfo)) {
			if(serviceReturnInfo.getData() != null) {
				shopNavigationGroup = (ShopNavigationGroup) serviceReturnInfo.getData();
				if(shopNavigationGroup.getGroupIsLeaf() == 0) {
					// 是根节点,找出节点子列表
					rtnInfo = shopNavigationGroupService.getShopNavicationGroupByParentId(shopNavigationGroup.getId());
					List<ShopNavigationGroup> list = (List<ShopNavigationGroup>) rtnInfo.getData();
					if(list.size() > 0) {
						for (ShopNavigationGroup item : list) {
							shopNavigationGroupService.deleteShopNavicationGroup(item.getId());
						}
					}
					// 最后删除根节点
					shopNavigationGroupService.deleteShopNavicationGroup(shopNavigationGroup.getId());
				}else {
					// 不是根节点，直接删除
					rtnInfo = shopNavigationGroupService.deleteShopNavicationGroup(Long.parseLong(groupId));
					
				}
			}
		}
		if (ServiceReturnUtils.isSuccess(rtnInfo)) {
			return ReturnUtils.success();
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

	@Override
	public RtnInfo updateShopNavicationGroup(String groupId, ShopNavigationGroupInput groupInput, Long endMerchantUserId) {
		
		EndMerchantUserBo endMerchantUserBo = null;

		// 获取当前merchantId对应的商家用户
		ServiceReturnInfo merchantReturnInfo = endMerchantUserService.getEndMerchantUserById(endMerchantUserId);
		if (ServiceReturnUtils.isSuccess(merchantReturnInfo)) {
			if (merchantReturnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				endMerchantUserBo = (EndMerchantUserBo) merchantReturnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
		
		
		ServiceReturnInfo rtnInfo = shopNavigationGroupService.updateShopNavicationGroup(groupId, groupInput, endMerchantUserBo);
		if (ServiceReturnUtils.isSuccess(rtnInfo)) {
			return ReturnUtils.success();
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}
	
	@Override
	public RtnInfo getGroupSkuCount(ShopNavSkuRequestBo requestBo) {
		ServiceReturnInfo rtnInfo = shopNavigationGroupService.getGroupSkuCount(requestBo);
		if (ServiceReturnUtils.isSuccess(rtnInfo)) {
			return ReturnUtils.success(rtnInfo.getData());
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}
	
	@Override
	public RtnInfo getSkuList(ShopNavSkuRequestBo requestBo) {
		ServiceReturnInfo rtnInfo = shopNavigationGroupService.getSkuList(requestBo);
		if (ServiceReturnUtils.isSuccess(rtnInfo)) {
			return ReturnUtils.success(rtnInfo.getData());
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

	@Override
	public RtnInfo addSku( Long groupId, Long shopId, Long skuId) {
		ServiceReturnInfo rtnInfo = shopNavigationGroupService.addSku(   groupId,  shopId,  skuId);
		if (ServiceReturnUtils.isSuccess(rtnInfo)) {
			return ReturnUtils.success(rtnInfo.getData());
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

	@Override
	public RtnInfo removeSku( Long groupId, Long shopId, Long skuId) {
		ServiceReturnInfo rtnInfo = shopNavigationGroupService.removeSku(   groupId,  shopId,  skuId);
		if (ServiceReturnUtils.isSuccess(rtnInfo)) {
			return ReturnUtils.success(rtnInfo.getData());
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}
}
